
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Xml;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.Managers;
using System.IO;
using Tathata.Common.BLL.EntityCollections;
using Tathata.Common.Utils;
namespace Tathata.Web.Administration
{
    public partial class Administration_Categories : BaseNopAdministrationPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string menu = GetCategories(0);
            menu = "<siteMapNode title=\"Categories\" url=\"" + string.Empty + "\">" + menu + "<siteMapNode  title=\"Add new category: \" url=\"categoryAdd.aspx\"></siteMapNode></siteMapNode>";
            ds.Data = menu;
            CategoryTreeView.DataBind();
        }

        protected string GetCategories(int ForParentEntityID)
        {
            StringBuilder tmpS = new StringBuilder(4096);

            CategoryCollection categoryCollection = CategoryManager.GetAllCategories(ForParentEntityID);

            for (int i = 0; i < categoryCollection.Count; i++)
            {
                Category category = categoryCollection[i];
                tmpS.Append("<siteMapNode  title=\"" + XmlHelper.XmlEncodeAttribute(category.Name) 
                    + "\" url=\"" + String.Format("categoryDetails.aspx?CategoryID=" + category.CategoryID.ToString()) + "\">");
                if (CategoryManager.GetAllCategories(category.CategoryID).Count > 0)
                    tmpS.Append(GetCategories(category.CategoryID));

                tmpS.Append("<siteMapNode  title=\"Add new category:\" url=\"CategoryAdd.aspx?ParentCategoryID=" + category.CategoryID.ToString() + "\"></siteMapNode>");

                ProductCategoryCollection productCategoryCollection = category.ProductCategories;
                tmpS.Append("<siteMapNode  title=\"Products:\" url=\"" + string.Empty + "\">");
                tmpS.Append("<siteMapNode  title=\"Add new product:\" url=\"ProductAdd.aspx?CategoryID=" + category.CategoryID.ToString() + "\"></siteMapNode>");
                foreach (ProductCategory productCategory in productCategoryCollection)
                {
                    Product product = productCategory.Product;
                    tmpS.Append("<siteMapNode  title=\"" + XmlHelper.XmlEncodeAttribute(product.Name) + "\" url=\"" + "ProductDetails.aspx?ProductID=" + product.ProductID.ToString() + "\">");
                    tmpS.Append("</siteMapNode>");
                }
                tmpS.Append("</siteMapNode>");
                tmpS.Append("</siteMapNode>");
            }
            return tmpS.ToString();
        }

        protected void btnExportXML_Click(object sender, EventArgs e)
        {
            //if (Page.IsValid)
            //{
            //    try
            //    {
            //        string fileName = string.Format("categories_{0}.xml", DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss"));
            //        string xml = ExportManager.ExportCategoriesToXML();
            //        CommonHelper.WriteResponseXML(xml, fileName);
            //    }
            //    catch (Exception exc)
            //    {
            //        ProcessException(exc);
            //    }
            //}
        }
    }
}